package com.lse_from.utils

import com.lse_from.component.Slf4j
import com.lse_from.component.Slf4j.Companion.log
import org.springframework.core.io.ClassPathResource
import org.springframework.core.io.Resource
import java.io.InputStream
import java.io.InputStreamReader
import java.nio.charset.Charset
import java.util.*
import kotlin.collections.HashMap

/**
 * 源文件工具
 *
 * @author 祁雪
 */
@Slf4j
class ResourceUtil {

    companion object {

        fun readFileByRes(name: String): String? {

            val resource: Resource = ClassPathResource(name)

            try {

                val inputStreamReader: InputStreamReader = InputStreamReader(resource.inputStream)

                inputStreamReader.use {
                    return it.readText()
                }

            } catch (e: Exception) {
                log.error("读取错误,请检查文件${name}是否存在", e)
            }
            return null
        }

        fun readInputByRes(name: String): InputStream? {

            val resource: Resource = ClassPathResource(name)
            try {

                return resource.inputStream

            } catch (e: Exception) {
                log.error("读取错误,请检查文件${name}是否存在", e)
            }
            return null
        }

        fun readLinesByRes(name: String, charset: Charset): List<String>? {

            val resource: Resource = ClassPathResource(name)

            try {
                val inputStreamReader: InputStreamReader = InputStreamReader(resource.inputStream, charset)

                inputStreamReader.use {
                    return it.readLines()
                }
            } catch (e: Exception) {
                log.error("读取错误,请检查文件${name}是否存在", e)
            }
            return null
        }

        fun readFileByRes(name: String, charset: Charset): String? {

            val resource: Resource = ClassPathResource(name)

            try {

                val inputStreamReader: InputStreamReader = InputStreamReader(resource.inputStream, charset)

                inputStreamReader.use {
                    return it.readText()
                }

            } catch (e: Exception) {
                log.error("读取错误,请检查文件${name}是否存在", e)
            }
            return null
        }


        fun getPropertiesByRes(name: String): HashMap<String, String>? {

            val properties = Properties()

            try {
                properties.load(ResourceUtil::class.java.getClassLoader().getResourceAsStream(name))
                val iterator: Iterator<String> = properties.stringPropertyNames().iterator()
                val map = hashMapOf<String, String>()
                while (iterator.hasNext()) {
                    val key = iterator.next()
                    map[key] = properties.getProperty(key)
                }
                properties.clear()

                return map
            } catch (e: Exception) {
                log.error("读取错误,请检查配置文件${name}是否存在", e)
            }

            return null
        }

    }

}